package cn.wolfcode.luowowo.wensite;

import cn.wolfcode.luowowo.wensite.util.UserInfoArgunmentResolver;
import cn.wolfcode.luowowo.wensite.web.interceptor.CheckLoginInterceptor;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@SpringBootApplication
@EnableDubbo
public class WebSite implements WebMvcConfigurer {
    @Bean
    public CheckLoginInterceptor checkLoginInterceptor() {
        return new CheckLoginInterceptor();
    }

    @Bean
    public UserInfoArgunmentResolver userInfoArgunmentResolver() {
        return  new UserInfoArgunmentResolver();
    }

    /**
     * 这个方法就等于在mvc配置文件中配置拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(checkLoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/login.html")
                .excludePathPatterns("/regist.html")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/styles/**");

    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userInfoArgunmentResolver());
    }

    public static void main(String[] args) {
        SpringApplication.run(WebSite.class, args);
    }
}
